package com.bjpowernode.money.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.money.Constant;
import com.bjpowernode.money.Result;
import com.bjpowernode.money.model.FinanceAccount;
import com.bjpowernode.money.model.User;
import com.bjpowernode.money.service.FinanceAccoutService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class FinanceAccountController {

    @Reference(interfaceClass = FinanceAccoutService.class,version = "1.0.0",timeout = 20000,check = false)
    FinanceAccoutService financeAccoutService;

    @RequestMapping("/loan/page/financeAccount")
    @ResponseBody
    public Object financeAccount(HttpServletRequest request){

        User user = (User) request.getSession().getAttribute(Constant.LOGIN_USER);
        //先判断是否登录
        if(!ObjectUtils.allNotNull(user)){
            //说明没有登录 返回错误信息
            return Result.error(-1,"请先登录");
        }

        //根据uid查询所对应的账号余额信息
        FinanceAccount financeAccount = financeAccoutService.queryFinanceAccountByUserId(user.getId());
        if (!ObjectUtils.allNotNull(financeAccount)){
            return Result.error(0,"服务器繁忙，请稍后再试");
        }

        request.getSession().setAttribute("availableMoney",financeAccount.getAvailableMoney());

        //返回的时候 直接把该用户的账户余额一并放在信息里返回
        return Result.success(financeAccount.getAvailableMoney()+"");


    }
}
